<template>
	<view class="mainpadding2">
		
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">名稱</view>
			<input type="text" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入名稱" v-model="name">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">充值金額</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入充值金額" v-model="price">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">贈送金額</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入贈送金額" v-model="send_price">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">發票品項</view>
			<input type="text" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入發票品項" v-model="recharge_name">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius" @click="selectrole()">
			<view class="hei_text bold titletext">可用分店</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" disabled="" style="text-align: right;" placeholder="請選擇可用分店" :value="rallshop.length?'已選擇':''">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius"  @click="messageTime.timeShow=true;timevalue=1">
			<view class="hei_text bold titletext">活動開始時間</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled="" v-model="start_time" placeholder="請選擇活動開始時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius"  @click="messageTime.timeShow=true;timevalue=2">
			<view class="hei_text bold titletext">活動結束時間</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled="" v-model="end_time" placeholder="請選擇活動結束時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		 <timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
		<view class="submitbtn margin_top" @click="submit">確認</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name:"",
				price:"",
				send_price:"",
				recharge_name:"",
				rallshop:[],
				start_time:"",
				end_time:"",
				messageTime:{
					timeShow:false,
					title:"選擇時間",
					mode:"datetime",//date日期，datetime年月日时分秒，time時間，year-month年月
				},
				timevalue:1,//1領取開始，3領取結束
				listid:"",
				type:"1",
				issubmit:false
			}
		},
		onLoad(options) {
			if(options.id){
				this.listid = options.id
				uni.setNavigationBarTitle({
					title:"編輯儲值訊息"
				})
				this.getDetail()
			}
			uni.$on("checkfd",data=>{
				this.rallshop = data
			})
		},
		onUnload() {
			uni.$off("checkfd")
		},
		methods: {
			getDetail(){
				this.http.request("/api/shop/rechargeItemDetail", 'POST', {
					id:this.listid
				}).then(res=>{
					let data = res.data
					this.name=data.name
					this.price=data.price
					this.send_price=data.send_price
					this.recharge_name = data.recharge_name
					this.start_time=data.start_time_text
					this.end_time=data.end_time_text
					let arr = []
					if(data.fenshop_id){
						let arr1 = data.fenshop_id.split(',')
						arr1.forEach(item=>{
							arr.push({
								id:"",
								num:0,
								shop_id:item,
							})
						})
					}
					this.rallshop = arr
				})
			},
			selectrole(){
				let data = {
					type:1,
					rallshop:this.rallshop,
					get_type:0
				}
				uni.navigateTo({
					url:"/pages/workbench/fendianlb?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
			timeConfirm(e){
				if(this.timevalue == 1){
					this.start_time = e
				}
				if(this.timevalue == 2){
					this.end_time = e
				}
			},
			async submit(){
				if(this.name==""){
					this.http.toast("請輸入名稱")
					return false
				}
				if(this.price==""){
					this.http.toast("請輸入充值金額")
					return false
				}
				if(this.send_price==""){
					this.http.toast("請輸入贈送金額")
					return false
				}
				if(this.recharge_name==""){
					this.http.toast("請輸入發票品項")
					return false
				}
				if(this.rallshop.length==0){
					this.http.toast("請選擇可用分店")
					return false
				}
				if(this.start_time==""){
					this.http.toast("請選擇活動開始時間")
					return false
				}
				if(this.end_time==""){
					this.http.toast("請選擇活動結束時間")
					return false
				}
				if(this.issubmit) return
				this.issubmit = true
				let rallshop = this.rallshop
				let fenshop_arr = []
				rallshop.forEach(item=>{
					fenshop_arr.push(item.shop_id)
				})
				fenshop_arr.push(uni.getStorageSync("shop_id"))
				let datamsg = {
					shop_id:uni.getStorageSync("shop_id"),
					name:this.name,
					price:this.price,
					send_price:this.send_price,
					recharge_name:this.recharge_name,
					fenshop_id:fenshop_arr.join(),
					start_time:this.start_time,
					end_time:this.end_time,
				}
				let url = "/api/shop/addRechargeItem"
				if(this.listid){
					url = "/api/shop/editRechargeItem"
					this.$set(datamsg,"id",this.listid)
				}
				uni.showLoading({
					mask:true,
					title:"請稍後"
				})
				let res = await this.http.request(url, 'POST', datamsg)
				uni.hideLoading()
				if(res.code==1){
					this.http.toast(res.msg)
					setTimeout(()=>{
						uni.navigateBack(1)
					},500)
				}else{
					this.issubmit = false
					this.http.toast(res.msg)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
::v-deep.u-radio-group {
		flex: none !important;
	}
</style>